<?php

class media_folders extends Model {

	public $folder_id = false;
public $folder_parent_id = false;
public $folder_title = false;
public $user_id = false;
public $folder_epoch = false;



    function media_folders()
    {
        // Call the Model constructor
        parent::Model();
		$this->_table_name = 'media_folders';
		$this->_primary_key = 'folder_id';
    }
	public function get_folder_tree($aSetVars = array())
	{
		$this->clear();
		
		if (count($aSetVars) > 0)
		{
			foreach ($aSetVars as $vVar => $vVal)
			{
				$this->$vVar = $vVal;	
			}	
		}
		
		$aOptions = array(
					'order_by' => array('folder_title' => 'ASC')
		);
		
		$qFolders = $this->get($aOptions);
		$iFolders = $qFolders->num_rows();
		
		
		$aFolders = array();
		
		if ($iFolders > 0)
		{
			for ($i = 0; $i < $iFolders; $i++)
			{
				$aFolders[$i]['folder'] = $qFolders->row($i);
				$aSubFolders = $this->get_folder_tree(array('folder_parent_id' => $qFolders->row($i)->folder_id));
				$iSubFolders = count($aSubFolders);
				
				if ($iSubFolders > 0)
				{
					$aFolders[$i]['sub_folders'] = $aSubFolders;
				}
				
			}	
		}
		
		
		return $aFolders;
		
		
	}	
	
	
	
	public function build_folder_tree($aMediaFolders, $oSite = null)
	{
		$iMediaFolders = count($aMediaFolders);
		$sContent = '';
		

		for ($i = 0; $i < $iMediaFolders; $i++)
		{
			$sChildContent = '';
			
			if (array_key_exists('sub_folders', $aMediaFolders[$i]) === true)
			{
				$sChildContent = $this->build_folder_tree($aMediaFolders[$i]['sub_folders']);
			}
			
			$data = array(
					'sLiClass' => 'folder_tree_node',
					'sLiId' => 'folder_tree_node_'.$aMediaFolders[$i]['folder']->folder_id,
					'sSpanHtml' => $aMediaFolders[$i]['folder']->folder_title,
					'sChildContent' => $sChildContent
			);
			
			
			$sContent .= $this->load->view('platform/'.$this->sPlatform.'/admin/media/build_li', $data, true);
			
		}
		
		return $sContent;
		
	}
	

}

?>